/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Interfaces.Imaging;
using MyPhotoIndex.ImageBank;

namespace MyPhotoIndex
{
    public partial class EditImageCategoriesForm : FormTemplate
    {
        private IImageCategoryCollection m_categoryCollection;
        private ImageList m_colorsImageList;

        public EditImageCategoriesForm(ImageList colorsImageList, IImageCategoryCollection categoryCollection):
            base()
        {
            InitializeComponent();

            m_categoryCollection = categoryCollection;
            m_colorsImageList = colorsImageList;

            this.Load += new EventHandler(EditImageCategoriesForm_Load);
            categoriesListView.AfterLabelEdit += new LabelEditEventHandler(categoriesListView_AfterLabelEdit);
        }

        void categoriesListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            ListView listView = sender as ListView;

            if (string.IsNullOrEmpty(e.Label) == true)
            {
                e.CancelEdit = true;
                return;
            }

            ListViewItem lvi = listView.Items[e.Item];
            if (string.Compare(e.Label.Trim(), lvi.Text, false) != 0)
            {
                IImageCategory category = (IImageCategory)lvi.Tag;
                category.Description = e.Label.Trim();
                ImageCategoryCollection.Instance.Save(category);
            }
        }

        void EditImageCategoriesForm_Load(object sender, EventArgs e)
        {
            foreach (IImageCategory category in m_categoryCollection)
            {
                categoriesListView.SmallImageList = m_colorsImageList;
                ListViewItem newItem = categoriesListView.Items.Add(category.Description, category.ColorIndex);
                newItem.Tag = category;
            }

            //colorsListView.SmallImageList = m_colorsImageList;
            //int i = 0;
            //foreach (Image colorImage in m_colorsImageList.Images)
            //{
            //    colorsListView.Items.Add(String.Empty, i++);
            //}
        }
    }
}